package com.example.provider.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.common.base.BaseController;
import com.example.common.base.Paging;
import com.example.common.base.Result;
import com.example.common.model.User;
import com.example.common.model.UserFeedback;
import com.example.common.model.condition.UserFeedbackCondition;
import com.example.provider.service.UserFeedbackService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;



/**
 * Controller
 * 
 * @author bianj
 * @version 1.0.0 2021-05-09
 */
@Api(tags = "user_feedback服务")
@RestController
@RequestMapping("/userFeedback")
public class UserFeedbackController extends BaseController {
    @Autowired
    private UserFeedbackService userFeedbackService;

    @ApiOperation(value = "根据查询参数分页查询列表")
    @ApiImplicitParam(name = "condition", value = "查询条件", required = true, dataType = "UserFeedbackCondition", paramType = "body")
    @PostMapping("/list")
    public Paging<UserFeedback> list(@RequestBody UserFeedbackCondition condition) {
        IPage<UserFeedback> page = userFeedbackService.findUserFeedbackByCondition(condition);
        return Paging.buildPaging(page);
    }

    @ApiOperation(value = "根据主键ID查询信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @GetMapping(value = "/get/{id}")
    public Result<UserFeedback> get(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要查询的数据！");
        }
        UserFeedback userFeedback = userFeedbackService.getUserFeedbackById(id);
        return Result.ok(userFeedback);
    }

    @ApiOperation(value = "新增信息")
    @ApiImplicitParam(name = "userFeedback", value = "", required = true, dataType = "UserFeedback", paramType = "body")
    @PostMapping("/add")
    public Result<?> add(@RequestBody UserFeedback userFeedback) {
        boolean bool = userFeedbackService.addUserFeedback(userFeedback);
        if (bool) {
            return Result.ok(userFeedback);
        }
        return Result.failed();
    }

    @ApiOperation(value = "修改信息")
    @ApiImplicitParam(name = "userFeedback", value = "", required = true, dataType = "UserFeedback", paramType = "body")
    @PutMapping(value = "/update")
    public Result<?> update(@RequestBody UserFeedback userFeedback) {
        Integer id = userFeedback.getId();
        if (id == null) {
            return Result.failed("请选择需要修改的数据！");
        }
        boolean bool = userFeedbackService.updateUserFeedback(userFeedback);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID删除")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userFeedbackService.deleteUserFeedbackById(id);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID列表批量删除")
    @ApiImplicitParam(name = "idList", value = "主键ID列表", required = true, allowMultiple = true, paramType = "body")
    @DeleteMapping("/deleteList")
    public Result<?> deleteList(@RequestBody List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = userFeedbackService.deleteUserFeedbackByIds(idList);
        return Result.okOrFailed(bool);
    }

    @PostMapping("/save")
    public Map<String,Object> saveFeedback(@RequestParam(value = "types") String types,
                                           @RequestParam(value = "content") String content,
                                           @RequestParam(value = "relation") String relation){
        Map<String,Object> map = new HashMap<>();
        UserFeedback userFeedback = new UserFeedback();
        User user = new User();
        System.err.println();
        userFeedback.setTypes(types);
        userFeedback.setContent(content);
        userFeedback.setRelation(relation);
        userFeedback.setFeedBackTime(new Date());
        userFeedback.setUserId(user.getUserId());
//        Boolean b= userFeedbackService.addUserFeedback(userFeedback);
        map.put("userFeedback",userFeedbackService.addUserFeedback(userFeedback));
        return map;
    }
}